﻿using System.Web.Mvc;
using ALinq.BBS.Models;
using System.Web.Security;
using System.Diagnostics;
using ALinq.BBS.App_GlobalResources;
using System.Dynamic;
using System;

namespace ALinq.BBS.Controllers
{
    partial class HomeController
    {
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                MembershipCreateStatus status;
                var user = Membership.CreateUser(model.UserName, model.Password, model.Email,
                                                 model.PasswordQuestion, model.PasswordAnswer, false, out status);
                if (status == MembershipCreateStatus.Success)
                {
                    var code = GetCode(user.CreationDate);
                    var returnUrl = Url.RouteUrl(RouteNames.Home, new { action = "ActiveAccount", user.UserName, code });
                    var registerEmail = new RegisterEmail(user.Email, returnUrl);
                    var emailer = new Emailer();
                    emailer.Send(registerEmail);

                    return RedirectToAction("ActiveAccount", "Home", new { user.Email });
                }
                else
                {
                    switch (status)
                    {
                        case MembershipCreateStatus.DuplicateEmail:
                            ModelState.AddModelError("", Messages.EmailDuplicate);
                            break;
                        case MembershipCreateStatus.DuplicateUserName:
                            ModelState.AddModelError("UserNameRegistered", Messages.UserNameRegistered);
                            break;
                    }
                }
            }
            return View(model);
        }

        public static string GetCode(DateTime dateTime)
        {
            return MD5(dateTime.ToString("yyyMMddHHmm"));
        }

        static string MD5(string text)
        {
            Debug.Assert(text != null);
            //获取要加密的字段，并转化为Byte[]数组   
            byte[] data = System.Text.Encoding.Unicode.GetBytes(text.ToCharArray());
            //建立加密服务   
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            //加密Byte[]数组   
            byte[] result = md5.ComputeHash(data);
            //将加密后的数组转化为字段   
            string sResult = System.Text.Encoding.Unicode.GetString(result);
            //显示出来   
            return sResult.ToString();
        }

        [HttpGet]
        public ActionResult ValidateUserName(string username)
        {
            var user = Membership.GetUser(username);
            return Json(user == null, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult ValidateEmail(string email)
        {
            var userName = Membership.GetUserNameByEmail(email);
            return Json(userName == null, JsonRequestBehavior.AllowGet);
        }

        //public ActionResult ActiveAccount(string email)
        //{
        //    dynamic model = new ExpandoObject();
        //    model.Email = email;
        //    return View();
        //}

        public ActionResult ActiveAccount(string email, string code, string userName)
        {

            if (email != null)
            {
                dynamic model = new ExpandoObject();
                model.Email = email;
                return View(model);
            }
            else
            {
                MembershipUser user = Membership.GetUser(userName);
                if (user != null)
                {
                    var c = ALinq.BBS.Controllers.HomeController.GetCode(user.CreationDate);
                    if (code == c)
                    {
                        return View(true);
                    }
                }
                return View(false);
            }
        }
    }
}